<?php
include_once('Paypal_api.php');
$api = new Paypal_api();

$item_description = 'An Item';

/**
 * This is testing the recurring payments
 * 
 */
/*if(@$_GET['token'] == '') {

	// $api->set_debug_mode('debug');
	$api->set_express_checkout('0', 'http://dev.clearmediawebsites.co.uk/paypal_api/test.php', 'http://dev.clearmediawebsites.co.uk/paypal_api/test.php', $item_description);
	// $api->set_customer_billing_agreement('http://dev.clearmediawebsites.co.uk/paypal_api/test.php', 'http://dev.clearmediawebsites.co.uk/paypal_api/test.php');
	$result = $api->get_result();
	
	if($result == true) {

		$api->redirect($api->get_token());
	} else {
		echo 'There was a problem, the error returned was: ' . $api->get_error();
	}
	
} else {

	// $api->set_debug_mode('debug');
	$api->get_express_checkout_details(@$_GET['token']);
	$result = $api->get_result();
	
	if($result == true) {
		// $api->set_debug_mode('debug');
		$api->create_recurring_payments_profile($api->get_token(), get_next_wed(), $item_description, 11.99, 1, 'Month');	
		$profile_id = $api->get_profile_id();
		
		$api->manage_recurring_payments_profile_status($api->get_profile_id(), 'cancel', 'autocanceled');
	}
}*/

/**
 *
 *
 *
 */
function percentage_of($percentage, $number)
{
	$number = $number / 100;
	$value = $number * $percentage;
	
	return $value;
}

$payment_amount = '10.99';

/**
 * This is testing the one-off payments
 *
 *
 */
if(@$_GET['token'] == '') {

	// $api->set_debug_mode('debug');
	$api->set_express_checkout($payment_amount, 'http://dev.clearmediawebsites.co.uk/paypal_api/test.php', 'http://dev.clearmediawebsites.co.uk/paypal_api/test.php', $item_description);
	// $api->set_customer_billing_agreement('http://dev.clearmediawebsites.co.uk/paypal_api/test.php', 'http://dev.clearmediawebsites.co.uk/paypal_api/test.php');
	$result = $api->get_result();
	
	if($result == true) {

		$api->redirect($api->get_token());
	} else {
		echo 'There was a problem, the error returned was: ' . $api->get_error();
	}
	
} else {

	// $api->set_debug_mode('debug');
	$api->get_express_checkout_details(@$_GET['token']);
	$result = $api->get_result();
	
	$discount_percentage 				= 10;
	$payment_amount						= ($payment_amount - percentage_of($discount_percentage, $payment_amount));	// This is the new order total
	
	if($result == true) {
		$api->set_debug_mode('debug');
		$payer_id = $api->get_payer_id();
		
		$api->do_express_checkout_payment(@$_GET['token'], $payer_id, $payment_amount);
	}

}

/**
 * gets next wed, but if next wed is 1 day away it will look forward to
 * next week, in order to ensure delivery is made successfully
 *
 * @param void
 * @return date
 */
function get_next_wed()
{
	$today = date('Y-m-d');
	$next_wed = date('Y-m-d', strtotime('next Wed'));
	
	$diff = date_diff($today, $next_wed, $token = '-');
	
	if($diff >= -1) {
		$next_wed = date('Y-m-d', strtotime('Wed next week'));
	}	
	
	return $next_wed;
}


/**
 * date_diff()
 *
 * expects date params to be presented Y-m-d (e.g. 2008-08-31)
 *
 * @param string [$start_date] the date to start from
 * @param string [$end_date] the final date
 * @param string $token the character dividing days months and years e.g. d-m-Y -> token = -
 * @return string difference between two dates in days
 */
function date_diff($start_date, $end_date, $token = '-')
{
	$date_parts1 = explode($token, $start_date);
	$date_parts2 = explode($token, $end_date);
	
	if(function_exists('gregoriantojd')) {
		// gregoriantojd(m, d, y) = american style
		$start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
		
		return $start_date - $end_date;
		
	} else {
		$start_date = mktime(0, 0, 0, $date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date = mktime(0, 0, 0, $date_parts2[1], $date_parts2[2], $date_parts2[0]);
		
		$difference = $start_date - $end_date;
		
		return floor($difference/(60*60*24));
	}
}